#include <Wire.h>
#include "infos.h"

#define GPM 0x68  	    // Adresse du peripherique du module gps

int led = 13;
int onModulePin = 2;        // Le pin qui permet de switch on/off le module gsm
char inchar;               // le char en entree de lecture sur le bus serie

byte Address;               // Variables des registres du module gps
byte Data;    

char coor[24];               //string dans laquelle on stock les coordonnees gps
char receiver[10];          //numero du demandeur de l'info     

void switchModule(){
  digitalWrite(onModulePin,HIGH);
  delay(2000);
  digitalWrite(onModulePin,LOW);  
  delay(2000);
}

void setup(){

  pinMode(led, OUTPUT);
  pinMode(onModulePin, OUTPUT);

  Wire.begin();
  Serial.begin(19200);
  pulse(led);                        // Indicateur visuel sur la board arduino
  digitalWrite(led, LOW);
  switchModule();

  for (int i=0;i<2;i++){            //On attends que le module gsm s'allume...
    delay(5000);                        
  } 

  Serial.print("AT+CPIN=");
  Serial.print(34,BYTE);
  Serial.print(pin_code);
  Serial.println(34,BYTE);
  delay(2000);

  Serial.println("AT+CMGF=1");         // passage en mode texte pour les SMS
  Serial.println("AT+CNMI=2,2,0,0");   // redirection des SMS dans le port serie
  pulse(led);
  refresh_coor();
}

void parse_receiver(){
  inchar = Serial.read();
  delay(10);
  if (inchar == '+')
  {
    delay(10);
    inchar = Serial.read();
    if (inchar=='3') 
    {
      delay(10);
      inchar = Serial.read();
      if (inchar=='3') //Si on recoit la chaine "+33" (debut du numero)
      {
        delay(10);
        receiver[0] = '0';
        int i;
        for(i = 1; i < 10; i++){
          inchar = Serial.read();
          receiver[i] = inchar;
        }
      }
    }

  }
}

void loop(){
  //If a character comes in from the cellular module...
  if(Serial.available() > 0)
  {

    parse_receiver();

    inchar = Serial.read();
    delay(10);
    if (inchar == '#')
    {
      delay(10);
      inchar = Serial.read();
      if (inchar=='1') //Si on recoit la chaine "#1" (exemple de commande)
      {
        delay(10);

        //if(positionInconnue()){
        // send_sms_inconnu();
        //}

        //Attends que le gps soit lock
        //while(positionInconnue()){    
        //  delay(1000);
        //}
        pulse(led);
        refresh_coor();
        send_sms_coor();

      }
    }
  }
  delay(10);
}

void send_sms_coor(){
  delay(1500);
  Serial.print("AT+CMGS=");
  Serial.print(34,BYTE);
  delay(1500); 
  int i;
  for(i = 0; i < 10; i++)
    Serial.print(receiver[i]);
  Serial.println(34,BYTE);
  delay(10);
  Serial.print("GPS ");
  delay(10);
  for(i = 0; i < 24; i++){
    Serial.print(coor[i]);
    delay(10);
  }
  delay(500);
  Serial.print(0x1A,BYTE);
  delay(1500);
}

void send_sms_inconnu(){
  delay(200);
  Serial.print("AT+CMGS=");
  Serial.print(34,BYTE);
  int i;
  for(i = 0; i < 10; i++)
    Serial.print(receiver[i]);
  Serial.println(34,BYTE);
  Serial.print("Module GPS en attente de localisation... Envoi des coordonnees ASAP.");
  Serial.print(0x1A,BYTE);
  delay(1500);        

}

void pulse(int output){
  for(int i = 0; i < 10; i++){
    digitalWrite(output, LOW);
    delay(50);
    digitalWrite(output, HIGH);
    delay(50);
  }

}

void refresh_coor(){

  Address = 14;                                       // Point to Latitude 1 register
  coor[0] = GetSingle()+48;                                 // Read registers from GPM
  Address = 15;                                       // Point to Latitude 2 register
  coor[1] = GetSingle()+48;                                 // Read registers from GPM
  coor[2]=' ';
  Address = 16;                                       // Point to Latitude 3 register
  coor[3] = GetSingle()+48;                                // Read registers from GPM
  Address = 17;                                       // Point to Latitude 4 register
  coor[4] = GetSingle()+48;                                 // Read registers from GPM
  coor[5] = '.';
  Address = 18;                                       // Point to Latitude 5 register
  coor[6] = GetSingle()+48;                                 // Read registers from GPM
  Address = 19;                                       // Point to Latitude 6 register
  coor[7] = GetSingle()+48;                                 // Read registers from GPM
  Address = 20;                                       // Point to Latitude 7 register
  coor[8] = GetSingle()+48;                                 // Read registers from GPM
  Address = 21;                                       // Point to Latitude 8 register
  coor[9] = GetSingle()+48;                                 // Read registers from GPM
  Address = 22;                                       // Point to Latitude character register
  coor[10] = GetSingle();                                 // Read registers from GPM

  coor[11]=' ';

  Address = 23;                                       
  coor[12] = GetSingle()+48;                      
  Address = 24;                                       
  coor[13] = GetSingle()+48;                         
  Address = 25;                                       
  coor[14] = GetSingle()+48;
  coor[15] = ' ';
  Address = 26;                                       // Point to Longitude 4 register
  coor[16] = GetSingle()+48;
  Address = 27;                                       // Point to Longitude 5 register
  coor[17] = GetSingle()+48;
  coor[18] = '.';
  Address = 28;                                       // Point to Longitude 6 register
  coor[19] = GetSingle()+48;
  Address = 29;                                       // Point to Longitude 7 register
  coor[20] = GetSingle()+48;
  Address = 30;                                       // Point to Longitude 8 register
  coor[21] = GetSingle()+48;
  Address = 31;                                       // Point to Longitude 9 register
  coor[22] = GetSingle()+48;
  Address = 32;                                       // Point to Longitude character register
  coor[23] = GetSingle();
}

int GetSingle(){				      // Get single register value from GPM

  int Value = 0; 

  Wire.beginTransmission(GPM);
  Wire.send(Address);				      // Send register start address
  Wire.endTransmission();

  Wire.requestFrom(GPM, 1);			      // Request register
  while(Wire.available() < 1);			      // Wait for single byte
  Value = Wire.receive();                             // and store

    return(Value);	                          
}

int positionInconnue()
{
  int statut;
  Address = 112;
  statut = GetSingle();
  return ((statut>>4)%2);
}



















